package com.xps.charmingshop.view.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xps.charmingshop.R;
import com.xps.charmingshop.bean.MapBean;
import com.xps.charmingshop.utils.AppCommond;
import com.xps.charmingshop.utils.MapApiConst;
import com.xps.charmingshop.view.base.BaseActivity;

import net.daum.mf.map.api.CameraUpdateFactory;
import net.daum.mf.map.api.MapPOIItem;
import net.daum.mf.map.api.MapPoint;
import net.daum.mf.map.api.MapReverseGeoCoder;
import net.daum.mf.map.api.MapView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by MI on 2018/1/11.
 */

public class MapActivity extends BaseActivity implements MapView.MapViewEventListener, MapReverseGeoCoder.ReverseGeoCodingResultListener {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.daumMapView)
    MapView daumMapView;
    @BindView(R.id.Text_Address)
    TextView TextAddress;
    private MapReverseGeoCoder reverseGeoCoder = null;

    @Override
    protected int getLayout() {
        return R.layout.activity_map;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("选择地址");
        actionbarTvTitle.setTextColor(getResources().getColor(R.color.white));
    }

    @Override
    protected void init(Bundle inBundle) {
        ButterKnife.bind(this);
        daumMapView.setDaumMapApiKey(MapApiConst.DAUM_MAPS_ANDROID_APP_API_KEY);
        daumMapView.setMapViewEventListener(this);
        daumMapView.moveCamera(CameraUpdateFactory.newMapPoint(MapPoint.mapPointWithGeoCoord(37.554923, 126.987220)));
        reverseGeoCoder = new MapReverseGeoCoder(MapApiConst.DAUM_MAPS_ANDROID_APP_API_KEY, daumMapView.getMapCenterPoint(), this, this);
        reverseGeoCoder.startFindingAddress();
    }

    @OnClick({R.id.actionbar_iv_back, R.id.Text_Address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.Text_Address:
                AppCommond appCommond = new AppCommond.Builder()
                        .setOpration(AppCommond.CLOSE_ACTIVITY)
                        .setRequestcloseAcname(MainActivity.class.getSimpleName())
                        .build();
                EventBus.getDefault().post(appCommond);
                finish();
                break;
        }
    }

    @Override
    public void onMapViewInitialized(MapView mapView) {
        // MapView had loaded. Now, MapView APIs could be called safely.
        mapView.setMapCenterPointAndZoomLevel(MapPoint.mapPointWithGeoCoord(37.554923, 126.987220), 8, true);
        MapPOIItem poiItem1 = new MapPOIItem();
        poiItem1.setItemName("POI1");
        poiItem1.setMapPoint(MapPoint.mapPointWithGeoCoord(37.554923, 126.987220));
        poiItem1.setMarkerType(MapPOIItem.MarkerType.BluePin);
        mapView.addPOIItem(poiItem1);
    }

    @Override
    public void onMapViewCenterPointMoved(MapView mapView, MapPoint mapCenterPoint) {
        mapView.removeAllPOIItems();
        MapPoint.GeoCoordinate mapPointGeo = mapCenterPoint.getMapPointGeoCoord();
        MapPOIItem poiItem1 = new MapPOIItem();
        poiItem1.setItemName("POI1");
        poiItem1.setTag(0);
        poiItem1.setMapPoint(MapPoint.mapPointWithGeoCoord(mapPointGeo.latitude, mapPointGeo.longitude));
        poiItem1.setMarkerType(MapPOIItem.MarkerType.BluePin);
        mapView.addPOIItem(poiItem1);
    }


    @Override
    public void onMapViewZoomLevelChanged(MapView mapView, int zoomLevel) {
        MapPoint.GeoCoordinate mapPointGeo = mapView.getMapCenterPoint().getMapPointGeoCoord();
    }

    @Override
    public void onMapViewSingleTapped(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        MapPoint.PlainCoordinate mapPointScreenLocation = mapPoint.getMapPointScreenLocation();
    }

    @Override
    public void onMapViewDoubleTapped(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        MapPoint.PlainCoordinate mapPointScreenLocation = mapPoint.getMapPointScreenLocation();
    }

    @Override
    public void onMapViewLongPressed(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        MapPoint.PlainCoordinate mapPointScreenLocation = mapPoint.getMapPointScreenLocation();
    }

    @Override
    public void onMapViewDragStarted(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        MapPoint.PlainCoordinate mapPointScreenLocation = mapPoint.getMapPointScreenLocation();
    }

    @Override
    public void onMapViewDragEnded(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        MapPoint.PlainCoordinate mapPointScreenLocation = mapPoint.getMapPointScreenLocation();
    }

    @Override
    public void onMapViewMoveFinished(MapView mapView, MapPoint mapPoint) {
        MapPoint.GeoCoordinate mapPointGeo = mapPoint.getMapPointGeoCoord();
        mapView.removeAllPOIItems();
        MapPOIItem poiItem1 = new MapPOIItem();
        poiItem1.setItemName("POI1");
        poiItem1.setTag(0);
        poiItem1.setMapPoint(MapPoint.mapPointWithGeoCoord(mapPointGeo.latitude, mapPointGeo.longitude));
        poiItem1.setMarkerType(MapPOIItem.MarkerType.BluePin);
        mapView.addPOIItem(poiItem1);
        MapBean.MapAddressLat = mapPointGeo.latitude + "";
        MapBean.MapAddressLng = mapPointGeo.longitude + "";
        reverseGeoCoder = new MapReverseGeoCoder(MapApiConst.DAUM_MAPS_ANDROID_APP_API_KEY, MapPoint.mapPointWithGeoCoord(mapPointGeo.latitude, mapPointGeo.longitude), this, this);
        reverseGeoCoder.startFindingAddress();
    }

    @Override
    public void onReverseGeoCoderFoundAddress(MapReverseGeoCoder rGeoCoder, String addressString) {
        TextAddress.setText(addressString.replace("-", "").replace("0", "").replace("9", "").replace("8", "").replace("7", "").replace("6", "").replace("5", "").replace("4", "").replace("3", "").replace("2", "").replace("1", ""));
        MapBean.MapAddress = addressString.replace("-", "").replace("0", "").replace("9", "").replace("8", "").replace("7", "").replace("6", "").replace("5", "").replace("4", "").replace("3", "").replace("2", "").replace("1", "");
        reverseGeoCoder = null;
    }

    @Override
    public void onReverseGeoCoderFailedToFindAddress(MapReverseGeoCoder rGeoCoder) {
        Log.i("asdasadss", "asdasdasdasd");
        reverseGeoCoder = null;
    }

}
